package com.lhb.redis.controller;

import com.lhb.redis.util.RedisUtil;
import jdk.nashorn.internal.ir.IdentNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.Date;

/**
 * redis实现文章投票功能
 */
@Controller
public class ArticleController {

    @Autowired
    private RedisUtil redisUtil;

    private static final long ONE_WEEK_IN_SECONDS = 7 * 86400; //时间
    private static final long VOTE_SCORE = 432; //分数

    public void articleVote(String user,String article) {
        long cutoff = new Date().getTime() - ONE_WEEK_IN_SECONDS;
        if(redisUtil.score("time:", article) < cutoff)
            return;
        String article_id = article.split(":")[-1];
        if(redisUtil.sHasKey("voted:"+article_id,user)==false){
            redisUtil.incrScore("score:",article,VOTE_SCORE);
            redisUtil.incrScore(article,"votes",1);
        }
    }
}
